﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UCore;

namespace Twin {
    public partial class frmDT : Form {
        
        public string GeneratedString { get; private set; }
        private string[] arg;
        //private string args;

        private int INST_BITS = 0;
        private int INST_OP = 1;
        private int INST_CMFG = 2;
        private int INST_DT = 3;
        

        private int opt;
        private int OPT_DT_LOCAL = 0;
        private int OPT_DT_MANUAL = 1;

        //DateTime dt = new DateTime();
        private XPort xport = new XPort();


        public frmDT(XPort xport, string content) {      
            InitializeComponent();
            
            GeneratedString = content;
            this.xport = xport;

            opt = OPT_DT_LOCAL;

            arg = content.Split(',');

            arg[INST_CMFG] = UI.init_cmbCmfg(cmbCmfg, arg[INST_CMFG]);
            //UI.init_cmbCom(cmbCom, Ini.defaultPort);
        }


        private void frmMisc_Load(object sender, EventArgs e) {
            this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.FixedSingle; // 设置窗体边框样式为固定单线框
            this.MaximizeBox = false;   // 移除最大化按钮
            this.MinimizeBox = false;   // 禁止调整大小

            TConsole.PositionFormAtTopRight(this);
        }

        
        private void cmbCmfg_SelectedIndexChanged(object sender, EventArgs e) {
            arg[INST_CMFG] = cmbCmfg.SelectedItem.ToString();
            if (arg[INST_CMFG].Equals("空")) {
                arg[INST_CMFG] = "";
            }
        }


        private DateTime get_hexDT() {
            DateTime dt;

            if (opt == OPT_DT_LOCAL) {
                dt = DateTime.Now;
            } else {
                dt = dateTimePicker1.Value;
            }
            return dt;
        }


        private void optDT_local_CheckedChanged(object sender, EventArgs e) {
            opt = OPT_DT_LOCAL;
        }


        private void optDT_manual_CheckedChanged(object sender, EventArgs e) {
            opt = OPT_DT_MANUAL;            
        }


        private void btnForceDT_Click(object sender, EventArgs e) {
            //int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            CInfo cInfo = new CInfo();

            Msg.setInfo_bits(ref cInfo, arg[INST_BITS]);

            //arg[INST_DT] = Common.hexYYMMDDHHmmSS(get_hexDT());
            cInfo.sDt = Common.hexYYMMDDHHmmSS(get_hexDT());

            cInfo.sOpArgs = arg[INST_OP] + "," + arg[INST_CMFG];    // + "," + arg[INST_DT];
            string wr_kvs = "";
            TConsole.sendAsProtocol(xport, ref cInfo, wr_kvs, "cobra");
        }


        private void btnNotifyDT_Click(object sender, EventArgs e) {
            //int iPort = int.Parse(cmbCom.Text.Replace("com", "")) - 1;
            CInfo cInfo = new CInfo();

            Msg.setInfo_bits(ref cInfo, arg[INST_BITS]);
            
            arg[INST_DT] = Common.hexYYMMDDHHmmSS(get_hexDT());
            //
            cInfo.sOpArgs = arg[INST_OP] + "," + arg[INST_CMFG] + "," + arg[INST_DT];
            string wr_kvs = "";
            TConsole.sendAsProtocol(xport, ref cInfo, wr_kvs, "cobra");
        }

        private void frmDT_Load(object sender, EventArgs e) {
            // 设置控件显示自定义格式的日期和时间
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            dateTimePicker1.Value = new DateTime(2000, 1, 1, 0, 0, 0);
            dateTimePicker1.ShowUpDown = true;
        }
    }
}
